package demo1;

import java.util.Arrays;

//深拷贝测试


class Mary implements Cloneable{
    public double num = 12.5;

    public Mary() {
    }

    public Mary(double num) {
        this.num = num;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

/**
 * Cloneable 标记接口
 * 表示当前类可以被克隆
 */
class Person implements Cloneable{
    private int id;
    private Mary mary = new Mary();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Mary getMary() {
        return mary;
    }

    public void setMary(Mary mary) {
        this.mary = mary;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person) super.clone();
        tmp.mary = (Mary) this.mary.clone();
        return tmp;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", mary=" + mary +
                '}';
    }
}

public class Test2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.setId(100);
        // 被 protected 修饰的在不同包中的子类调用父类方法时，需要用super
        Person person1 = (Person) person.clone();
        person1.getMary().num = 2;
        System.out.println(person.getMary().num);
        System.out.println(person1.getMary().num);
    }


}
